package cc.rengu.oltp.service.common.dao.impl;

import cc.rengu.jradp.mods.xml.XmlTree;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.dao.FusingCtrlInfoMapper;
import cc.rengu.oltp.service.common.entity.FusingCtrlInfo;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 熔断控制信息表操作接口实现类
 * Created by 王成 on 2020/2/10
 */
public class FusingCtrlInfoMapperImpl implements FusingCtrlInfoMapper {
    private final RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppParamConstant.OLTP_POOL_NAME);

    public FusingCtrlInfoMapperImpl() throws Exception {
        if (XmlTree.hasThreadTree()) {
            XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
            String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
            if (null != corporation && !corporation.isEmpty()) {
                String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
                dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppParamConstant.OLTP_POOL_NAME);
            }
        }
    }

    @Override
    public int insertFusigCtrlInfo(FusingCtrlInfo fusingCtrlInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "insertFusigCtrlInfo");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, fusingCtrlInfo, null);
        if (0 != iReturnCode) {
            rglog.error("登记熔断控制信息失败,instId:<{}>,fusingCtrlType:<{}>,fusingCtrlInst:<{}>,iReturnCode:<{}>", fusingCtrlInfo.getInstId(), fusingCtrlInfo.getFusingCtrlType(), fusingCtrlInfo.getFusingCtrlInst(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateFusingCtrlInfoByPrimaryKey(FusingCtrlInfo fusingCtrlInfo) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateFusingCtrlInfoByPrimaryKey");
        fusingCtrlInfo.setUpdateTime(DateUtil.getCurrentDateTime("yyyy-MM-dd HH:mm:ss.SSS"));
        int iReturnCode = dbsUtil.dbsExecuteTransactionSql(sqlStr, fusingCtrlInfo, null);
        if (0 != iReturnCode) {
            rglog.error("根据主键更新熔断控制信息失败,instId:<{}>,fusingCtrlType:<{}>,fusingCtrlInst:<{}>,iReturnCode:<{}>", fusingCtrlInfo.getInstId(), fusingCtrlInfo.getFusingCtrlType(), fusingCtrlInfo.getFusingCtrlInst(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public FusingCtrlInfo selectFusingCtrlInfoByPrimaryKey(String instId, String fusingCtrlType, String fusingCtrlInst) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectFusingCtrlInfoByPrimaryKey");
        FusingCtrlInfo fusingCtrlInfo = new FusingCtrlInfo();
        fusingCtrlInfo.setInstId(instId);
        fusingCtrlInfo.setFusingCtrlType(fusingCtrlType);
        fusingCtrlInfo.setFusingCtrlInst(fusingCtrlInst);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, fusingCtrlInfo, fusingCtrlInfo);
        if (0 != iReturnCode) {
            rglog.warn("根据主键查询熔断控制信息失败,instId:<{}>,fusingCtrlType:<{}>,fusingCtrlInst:<{}>,iReturnCode:<{}>", instId, fusingCtrlType, fusingCtrlInst, iReturnCode);
            return null;
        }
        return fusingCtrlInfo;
    }

    @Override
    public List<FusingCtrlInfo> selectAllFusingCtrlInfo(String instId) throws Exception {
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectAllFusingCtrlInfo");
        FusingCtrlInfo fusingCtrlInfo = new FusingCtrlInfo();
        fusingCtrlInfo.setInstId(instId);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, fusingCtrlInfo, FusingCtrlInfo.class);
        if (objectList.isEmpty()) {
            rglog.warn("根据法人行标识<{}>获取所有熔断控制信息失败!", instId);
            return null;
        }
        return objectList.stream().map(item -> (FusingCtrlInfo) (item)).collect(Collectors.toList());
    }
}
